package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dao.RelationshipSupplierMachineDao;
import com.fuhe.chen.vendingmachine.dao.UserDao;
import com.fuhe.chen.vendingmachine.dto.BookOrderStatus;
import com.fuhe.chen.vendingmachine.dto.UserDto;
import com.fuhe.chen.vendingmachine.pojo.Machine;
import com.fuhe.chen.vendingmachine.pojo.RelationshipSupplierMachine;
import com.fuhe.chen.vendingmachine.pojo.User;
import com.fuhe.chen.vendingmachine.service.IBookOrderService;
import com.fuhe.chen.vendingmachine.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserDao userDao;

    @Autowired
    IBookOrderService bookOrderService;

    @Autowired
    RelationshipSupplierMachineDao relationshipSupplierMachineDao;

    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }

    @Override
    public List<User> findAll() {
        return null;
    }

    @Override
    public User findOne(String name) {
        return userDao.findOne(name);
    }

    @Override
    public UserDto findUserInfoById(String name) {
        User byId = findOne(name);
        if(byId==null){
            return null;
        }
        BookOrderStatus bookOrderStatus = bookOrderService.getBookOrderStatus(byId.getId());

        UserDto user = new UserDto();
        user.setId(byId.getId());
        user.setName(byId.getName());
        user.setNickname(byId.getNickname());
        user.setPhone(byId.getPhone());
        user.setBookOrderStatus(bookOrderStatus);

        return user;
    }

    @Override
    public User findById(String user_id) {
        return  userDao.findById(user_id);
    }

    @Override
    public Boolean userCheck(String name) {
        Integer result = userDao.userCheck(name);
        if(result>0){
            return true;
        }
        return false;
    }

    @Override
    public String empower(String user_id, String role) {
        User byId = userDao.findById(user_id);
        if(byId==null){
            return "账号不存在";
        }
        if(role.equals(byId.getRole())){
            return "已拥有该权限";
        }
        userDao.updateRole(user_id,role);

        return "操作成功";
    }

    @Override
    public String distributeMachine(String supplier_id, Integer machine_id) {
        User byId = findById(supplier_id);
        if(byId==null){
            return "账号不存在";
        }
        if(!byId.getRole().equals("supplier")){
            return "该账号不是供货商账号";
        }
        RelationshipSupplierMachine byCondition = getRelationshipByMachineId(machine_id);
        if(byCondition!=null){
            return "该机器已经绑定供货商";
        }

        RelationshipSupplierMachine relationship = new RelationshipSupplierMachine();
        relationship.setSupplier_id(supplier_id);
        relationship.setMachine_id(machine_id);
        relationshipSupplierMachineDao.addRelationship(relationship);

        return MessageConstant.Success.DO_SUCCESS_MESSAGE;
    }

    @Override
    public void takeBackMachine(Integer relationship_id) {
        relationshipSupplierMachineDao.deleteRelationship(relationship_id);
    }

    @Override
    public RelationshipSupplierMachine getRelationshipByMachineId(Integer machine_id) {
        RelationshipSupplierMachine byCondition = relationshipSupplierMachineDao.findByMachineId(machine_id);
        return byCondition;
    }

    @Override
    public List<Machine> getMachineListBySupplierId(String supplier_id) {
        List<RelationshipSupplierMachine> bySupplierId = relationshipSupplierMachineDao.findBySupplierId(supplier_id);
        List<Machine> machines = new ArrayList<>();
        for (RelationshipSupplierMachine relationshipSupplierMachine : bySupplierId) {
            machines.add(relationshipSupplierMachine.getMachine());
        }
        return machines;
    }
}
